package com.woniuxy.config;

import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.BindingBuilder;
import org.springframework.amqp.core.BindingBuilder.TopicExchangeRoutingKeyConfigurer;
import org.springframework.amqp.core.FanoutExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

/**
 * Author: 马宇航
 * Do: 发布订阅模式，包含2个队列，和2个订阅者，发布者的交换机，要选择fanout(子网广播模式)
 *  在用两个bindingkey绑定队列到交换机
 * DateTime: 22/07/06/0006 下午 4:06
 * Component: 成都蜗牛学苑
 * <img src="https://woniumd.oss-cn-hangzhou.aliyuncs.com/java/mayuhang/202207032343928.png"/>
 **/
@Configuration
public class PublishshSubscribeConfig {
    /**
     * 创建两个队列
     * ChangeLog : 1. 创建 (22/07/06/0006 下午 4:09 [马宇航]);
     *
     * @return org.springframework.amqp.core.Queue
    */
    @Bean
    public Queue psQueue1(){
        return new Queue("psQueue1");
    }
    @Bean
    public Queue psQueue2(){
        return new Queue("psQueue2");
    }
    /**
     * 创建广播模式的交换机
     * ChangeLog : 1. 创建 (22/07/06/0006 下午 4:09 [马宇航]);
     *
     * @return org.springframework.amqp.core.FanoutExchange
    */
    @Bean
    public FanoutExchange fanoutExchange(){
        return new FanoutExchange("fanout");
    }
    /**
     * 队列和交换机绑定
     * ChangeLog : 1. 创建 (22/07/06/0006 下午 4:11 [马宇航]);
     *
     * @return org.springframework.amqp.core.Binding
    */
    @Bean
    public Binding binding1binding(){
        //绑定队列和广播交换机
        return BindingBuilder.bind(psQueue1()).to(fanoutExchange());
    }
    @Bean
    public Binding binding2binding(){
        //绑定队列和广播交换机
        return BindingBuilder.bind(psQueue2()).to(fanoutExchange());
    }
}
